/***
 * @title: sys_log ServiceImpl file
 * @author: seasonszx dev
 * @datetime: 2018-01-24 14:08:07
 * @version: 1.0.0
 */
package com.zx.platform.zanadu.manager.serviceimpl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zx.platform.zanadu.manager.mongo.bean.SysLogRepository;
import com.zx.platform.zanadu.manager.pojo.SysLog;
import com.zx.platform.zanadu.manager.service.SysLogService;

@Service
@Transactional
public class SysLogServiceImplMongodb implements SysLogService {


	@Autowired
	SysLogRepository sysLogRepository;

	@Autowired
	MongoTemplate mongoTemplate;

	// 基本信息新增服务
	@Override
	public int insert(SysLog info) {
		mongoTemplate.insert(info);
		return 1;
	}

	// 基本信息修改服务
	@Override
	public int update(SysLog info) {
		Query query = new Query();
		query.addCriteria(Criteria.where("uuid").is(info.getUuid()));
		Update update = new Update();

		Field[] fields = info.getClass().getDeclaredFields();
		try {
			for (Field field : fields) {
				field.setAccessible(true); // 设置些属性是可以访问的
				String key = field.getName(); // 得到此属性的名称
				if(key.equalsIgnoreCase("serialVersionUID")) {
					continue;
				}
				String Key = key.substring(0, 1).toUpperCase() + key.substring(1);  
				Method method = info.getClass().getMethod("get"+Key);
				Object val = method.invoke(info); // 得到此属性的值
				if(null != val && !val.toString().equalsIgnoreCase("")) {
					update.set(key, val);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		mongoTemplate.updateFirst(query, update, SysLog.class);
		return 0;
	}

	// 主键删除服务
	@Override
	public int deleteByPrimaryKey(String primarykey) {
		Query query = new Query();
		query.addCriteria(Criteria.where("uuid").is(primarykey));
		mongoTemplate.remove(query, SysLog.class);
		return 1;
	}

	// 主键查询对象服务
	@Override
	public SysLog selectByPrimaryKey(String primarykey) {
		Query query = new Query();
		query.addCriteria(Criteria.where("uuid").is(primarykey));
		return mongoTemplate.findOne(query, SysLog.class);
	}

	// 全查集合服务
	@Override
	public List<SysLog> selectAll() {
		return mongoTemplate.findAll(SysLog.class);
	}

	// 条件查询集合
	@Override
	public List<SysLog> selectByCondition(Map<String, Object> map) {
		Query query = new Query();

		for (Map.Entry<String, Object> entry : map.entrySet()) {
			String key = entry.getKey();
			Object val = entry.getValue();
			if(null != val && !val.toString().equalsIgnoreCase("")) {
				query.addCriteria(Criteria.where(key).regex(".*?" + val.toString() + ".*"));
			}
		}

		return mongoTemplate.find(query, SysLog.class);
	}

	// 分页条件查询集合
	@Override
	public List<SysLog> selectByPageCondition(Map<String, Object> map) {
		int pageNo = Integer.parseInt(map.get("pageNo").toString());
		int pageSize = Integer.parseInt(map.get("pageSize").toString());
		map.remove("pageNo");
		map.remove("pageSize");
		
		Query query = new Query();

		for (Map.Entry<String, Object> entry : map.entrySet()) {
			String key = entry.getKey();
			Object val = entry.getValue();
			if(null != val && !val.toString().equalsIgnoreCase("")) {
				query.addCriteria(Criteria.where(key).regex(".*?\\" + val.toString() + ".*"));
			}
		}

		return mongoTemplate.find(query.limit(pageSize).skip(pageSize*(pageNo-1)), SysLog.class);
	}

}
